
import { UITransform } from 'cc';
import { Sprite } from 'cc';
import { _decorator, Component, Label, Node, instantiate, EditBox, Button, tween, view, Tween, Vec3, find } from 'cc';
import { App } from '../../App';
import { EventConst } from '../../common/Common';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { PD } from '../../player/PlayerData';
import { LoadMg } from '../../tools/LoadMg';
import { showTip } from '../../tools/PopuUp';
import { ShowTip } from '../../tools/ShowTip';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

/* 开奖界面 */
@ccclass('LBLotteryPanel')
export class LBLotteryPanel extends Component {
    @property(Node)
    waitView: Node = null;  // 等待界面

    @property(Node)
    lotteryView: Node = null;// 开奖界面

    @property(Label)
    timeDJS1: Label = null;

    @property(Label)
    timeDJS2: Label = null;

    private viewState = 1   // [1]等待界面 [2]开奖界面
    private parentObj = null
    private data = null
    private timeEnd = false // 倒计时是否已经结束


    protected onLoad(): void {
        
    }

    start() {
       UtilTool.setScreen(this.node)
       this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())
    }

    update(deltaTime: number) {
       
    }

    /** 展示界面
     * @param state [1]等待界面 [2]开奖界面
      */
    showView(parentObj, data:any) {

        this.parentObj = parentObj
        this.viewState = data.status
        this.data = data

        this.waitView.active = data.status==1
        this.lotteryView.active = data.status==2

        if (data.status==1) {
            this.loadWaitView(data)
        }
        else if (data.status==2) {
            this.loadLotteryView(data)
        }
        else {
            this.node.destroy()
        }
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 等待界面 /////////////////////////////////////////////////////////////////////////////////////////
    /** 加载等待界面
     * @param data
      */
    loadWaitView(data) {
        let that = this
        UtilTool.nodeActive(this.waitView,5,0.16)

        // 开奖剩余时间倒计时
        UtilTool.timeCountdown(this.timeDJS1, null, data.countdown, function(){
            that.timeEnd = true
        })
    }

    /** 
     * 关闭
      */
    closeWaitView() {
        this.parentObj.clickClose()
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 等待界面 /////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 开奖界面 /////////////////////////////////////////////////////////////////////////////////////////
    /** 加载开奖界面
     * @param data
      */
    loadLotteryView(data) {
        let that = this
        UtilTool.nodeActive(this.lotteryView,5,0.16)
        
        // 开奖的球
        let ballSpr = find("di/ballbox/ballSpr",this.lotteryView)
        ballSpr.getComponent(Sprite).spriteFrame = this.parentObj.ballArr[parseInt(data.nums)]

        // 打开箱子动画
        let guang = this.lotteryView.getChildByName("guang")
        let boxSpr = this.lotteryView.getChildByName("openBox")
        tween(guang)
            .by(20.0, { angle: 3600 })
            .repeatForever()
            .start()

        setTimeout(() => {
            UtilTool.nodeActive(boxSpr,5,0.3)
            App.Audio.playOneShot("lottery",true)
        }, 0.5);

        let playNode = find("playNode",this.lotteryView)
        let Btn_back2 = find("Btn_back2",this.lotteryView)
        if (this.parentObj.isLottery(data.nums) && parseInt(data.nums)!=0) {
            // 击打幸运球
            playNode.active = true
            Btn_back2.active = false
            // 击打球剩余时间倒计时
            UtilTool.timeCountdown(this.timeDJS2, null, data.countdown, function(){
                that.timeEnd = true
            })

            // 如果已经击打过则置灰
            let Label = find("Btn_play/Label",playNode)
            let Label_have = find("Btn_play/Label_have",playNode)
            if (this.parentObj.isHited(data.nums)){
                Label.active = false
                Label_have.active = true
                UtilTool.imgToGray(find("Btn_play",playNode)) // 游戏类型置灰
                find("Btn_play",playNode).getComponent(Button).interactable = false
            }else{
                Label.active = true
                Label_have.active = false
            }
        }
        else {
            playNode.active = false
            Btn_back2.active = true
        }
        
    }

    /** 
     * 击打幸运球
      */
    clickPlayBall() {
        App.Audio.playOneShot("click")

        if (this.timeEnd) {
            this.node.destroy()
        } 
        else
        {
            this.parentObj.enterGame()
        }
    }

    /** 
     * 关闭
      */
    closeLotteryView() {
        App.Audio.playOneShot("click")
        this.node.destroy()
    }


}

